package com.broada.apm.indicator.api.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.broada.apm.dao.AlertDao;
import com.broada.apm.indicator.api.AlertService;
import com.broada.apm.model.Alert;
import com.broada.apm.model.ConfigItem;
import com.broada.apm.model.PagingList;

public class AlertServiceImpl implements AlertService{

	@Autowired
	AlertDao alertDao;
	
	@Override
	public void addAlert(Alert alert) {
		alertDao.addAlert(alert);
	}

	@Override
	public void updateAlert(Alert alert) {
		alertDao.updateAlert(alert);
	}

	@Override
	public void deleteAlert(Alert alert) {
		alertDao.deleteAlert(alert);
	}

	@Override
	public PagingList<Alert> getAlert(Alert alert, int pageIndex, int pageSize,
			Date startDate, Date endDate, String sortField, String sortBy) {
		return alertDao.getAlert(alert, pageIndex, pageSize, startDate, endDate, sortField, sortBy);
	}

	@Override
	public int getAlertCountBySeverity(int severity) {
		return alertDao.getAlertCountBySeverity(severity);
	}

	@Override
	public Alert getAlertById(String id) {
		return alertDao.getAlertById(id);
	}

	@Override
	public int getServerCountBySeverity(int severity) {
		return alertDao.getServerCountBySeverity(severity);
	}

	@Override
	public List<Alert> getAlertBySeverityAndServiceId(String id, int serverity) {
		return alertDao.getAlertBySeverityAndServiceId(id, serverity);
	}
	
	@Override
	public void saveAlertThreshold(ConfigItem configItem) {
		ConfigItem exist = alertDao.getAlertThreshold(configItem.getKey());
		if (exist == null)
			alertDao.addAlertThreshold(configItem);
		else
			alertDao.updateAlertThreshold(configItem);
	}

	@Override
	public void updateAlertThreshold(ConfigItem configItem) {
		alertDao.updateAlertThreshold(configItem);
	}

	@Override
	public ConfigItem deleteAlertThreshold(String key) {
		ConfigItem configItem = alertDao.getAlertThreshold(key);
		alertDao.deleteAlertThreshold(configItem);
		return configItem;
	}

	@Override
	public ConfigItem getAlertThreshold(String key) {
		return alertDao.getAlertThreshold(key);
	}
	
	@Override
	public List<ConfigItem> getAlertThresholdLikeKey(String key) {
		return alertDao.getAlertThresholdLikeKey(key);
	}

	@Override
	public int getAlertCountBySeverityCondition(int severity, String appId, String serviceId, Date startTime, Date endTime) {
		return alertDao.getAlertCountBySeverityCondition(severity, appId, serviceId, startTime, endTime);
	}

	@Override
	public Integer getAppAlertCount(String appId, Date startTime, Date endTime) {
		return alertDao.getAppAlertCount(appId, startTime, endTime);
//		return alertDao.getAlertCountBySeverityCondition(0, appId, startTime, endTime);
	}

	@Override
	public Map<String, Map<String, Long>> getAlertCountByType(String appId, String serviceId, Date startTime, Date endTime) {
		return alertDao.getAppAlertCountByType(appId, serviceId, startTime, endTime);
	}

	@Override
	public Map<String, Map<String, Long>> getAlertCountBySeverity(String appId, String serviceId, Date startTime, Date endTime) {
		return alertDao.getAlertCountBySeverity(appId, serviceId, startTime, endTime);
	}

	@Override
	public Map<String, Map<String, Object>> getAlertByServiceAndSeverity(List<String> servIdList, Date startTime, Date endTime, int severity) {
		return alertDao.getAlertByServiceAndSeverity(servIdList, startTime, endTime, severity);
	}

	@Override
	public Map<String, Map<String, Long>> getAlertCountBySeverity(String groupBy, Date startTime, Date endTime) {
		return alertDao.getAlertCountBySeverity(groupBy, startTime, endTime);
	}

	@Override
	public Map<String, Map<String, Long>> getAlertCountByType(String groupBy, Date startTime, Date endTime) {
		return alertDao.getAlertCountByType(groupBy, startTime, endTime);
	}

}
